<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>DHorse-简单易用的DevOps开发平台</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
	<link rel="icon" href="../images/favicon.ico">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        body {background-color:#DEDEDE;height:100%;width:100%;}
        #container{height:100%;width:100%;}
        input:-webkit-autofill {-webkit-box-shadow:inset 0 0 0 1000px #fff;background-color:transparent;}
        .admin-login-background {width:300px;height:300px;position:absolute;left:50%;top:30%;margin-left:-150px;margin-top:-100px;}
        .admin-header {text-align:center;margin-bottom:20px;color:#525252;font-weight:bold;font-size:40px}
        .admin-input {border-top-style:none;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;height:50px;width:300px;padding-bottom:0px;}
        #.admin-input::-webkit-input-placeholder {color:#1E9FFF}
        .layui-icon-username {color:#1E9FFF !important;}
        .layui-icon-username:hover {color:#9dadce !important;}
        .layui-icon-password {color:#1E9FFF !important;}
        .layui-icon-password:hover {color:#9dadce !important;}
        .admin-input-username {border-top-style:solid;border-radius:10px 10px 0 0;}
        .admin-input-verify {border-radius:0 0 10px 10px;}
        .admin-button {margin-top:20px;font-weight:bold;font-size:18px;width:300px;height:50px;border-radius:5px;background-color:#1E9FFF}
        .admin-icon {margin-left:260px;margin-top:10px;font-size:30px;}
        i {position:absolute;}
        .admin-captcha {position:absolute;margin-left:205px;margin-top:-40px;}
		.layui-this {color: #1E9FFF !important;}
		.layui-this:after{border-bottom: 2px solid #1E9FFF !important;}
    </style>
</head>
<body>
<div id="container">
	<div class="admin-login-background">
		<div class="admin-header">
			<span>登录</span>
		</div>
		<div class="layui-tab layui-tab-brief" lay-filter="login-handle">
			<ul class="layui-tab-title">
				<li class="layui-this" lay-id="normal-title" id="normal-title">普通</li>
			</ul>
			<div class="layui-tab-content">
				<div class="layui-tab-item layui-show">
					<form class="layui-form" action="">
						<div>
							<i class="layui-icon layui-icon-username admin-icon"></i>
							<input type="text" name="loginName" placeholder="请输入登录名" lay-verify="required" lay-reqtext="登录名不能为空" autocomplete="off" class="layui-input admin-input admin-input-username">
						</div>
						<div>
							<i class="layui-icon layui-icon-password admin-icon"></i>
							<input type="password" name="password" placeholder="请输入密码" lay-verify="required" lay-reqtext="密码不能为空" autocomplete="off" class="layui-input admin-input admin-input-verify">
						</div>
						<input type="hidden" name="loginSource" value="1">
						<button class="layui-btn admin-button" lay-submit="" lay-filter="normal-login">登 陆</button>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../js/common.js" charset="utf-8"></script>
<script src="https://wwcdn.weixin.qq.com/node/wework/wwopen/js/wwLogin-1.2.8.js"></script>
<script src="https://g.alicdn.com/dingding/h5-dingtalk-login/0.21.0/ddlogin.js"></script>
<script src="https://lf-package-cn.feishucdn.com/obj/feishu-static/lark/passport/qrcode/LarkSSOSDKWebQRCode-1.0.3.js"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
			$ = layui.$;
		var element = layui.element;

        // 登录过期的时候，跳出ifram框架
        if (top.location != self.location) top.location = self.location;
		
		//初始化ldap登录
		$.ajax({
			url: '/globalConfig/query/ldap',
			type: 'POST',
			dataType: "json",
			contentType: 'application/json;charset=UTF-8',
			data: JSON.stringify({"itemType": 1}),
			success: function(res){
				if(res.code != "000000"){
					return false;
				}
				if(!res.data || !res.data.url){
					return false;
				}
				//如果没有开启
				if(res.data.enable != 1){
					return false;
				}
				
				var content = '\
					<form class="layui-form" action="">\
						<div>\
							<i class="layui-icon layui-icon-username admin-icon"></i>\
							<input type="text" name="loginName" placeholder="请输入登录名" lay-verify="required" lay-reqtext="登录名不能为空" autocomplete="off" class="layui-input admin-input admin-input-username">\
						</div>\
						<div>\
							<i class="layui-icon layui-icon-password admin-icon"></i>\
							<input type="password" name="password" placeholder="请输入密码" lay-verify="required" lay-reqtext="密码不能为空" autocomplete="off" class="layui-input admin-input admin-input-verify">\
						</div>\
						<input type="hidden" name="loginSource" value="2">\
						<button class="layui-btn admin-button" lay-submit="" lay-filter="ldap-login">登 陆</button>\
					</form>\
				';
				
				element.tabAdd('login-handle', {
					title: 'LDAP',
					content: content,
					id: 'ldap-title'
				});
				element.tabChange('login-handle', 'ldap-title');
			},
			error: function(res){
				return false;
			}
		});
		
		//初始化企业微信登录
		$.ajax({
			url: '/globalConfig/query/wechat',
			type: 'POST',
			dataType: "json",
			contentType: 'application/json;charset=UTF-8',
			data: JSON.stringify({"itemType": 8}),
			success: function(res){
				if(res.code != "000000"){
					return false;
				}
				if(!res.data || !res.data.corpId){
					return false;
				}
				//如果没有开启
				if(res.data.enable != 1){
					return false;
				}
				
				element.tabAdd('login-handle', {
					title: '企业微信',
					content: '<div id="wx_reg"></div>',
					id: 'wechat-title'
				});
				element.tabChange('login-handle', 'wechat-title');
				var cssContent = 'data:text/css;base64,LmltcG93ZXJCb3gge21hcmdpbi10b3A6IC03JTt9Ci5pbXBvd2VyQm94IC5xcmNvZGUge3dpZHRoOiAyMDBweDt9Ci5pbXBvd2VyQm94IC50aXRsZSB7ZGlzcGxheTogbm9uZTt9Ci5pbXBvd2VyQm94IC5pbmZvIHtkaXNwbGF5OiBub25lO30KLnN0YXR1c19pY29uIHtkaXNwbGF577yabm9uZTt9';
				var wwLogin = new WwLogin({
					"id": "wx_reg",  
					"appid": res.data.corpId,
					"agentid": res.data.agentId,
					"redirect_uri": encodeURIComponent(window.location.protocol + '//'+ window.location.host +'/wechat'),
					"state": "sucess",
					"href": cssContent,
					"lang": "zh",
				});
			},
			error: function(res){
				return false;
			}
		});
		
		//初始化钉钉登录
		$.ajax({
			url: '/globalConfig/query/dingding',
			type: 'POST',
			dataType: "json",
			contentType: 'application/json;charset=UTF-8',
			data: JSON.stringify({"itemType": 10}),
			success: function(res){
				//如果没有开启
				if(res.code != "000000" || !res.data || res.data.enable != 1){
					return false;
				}
				
				element.tabAdd('login-handle', {
					title: '钉钉',
					content: '<div id="dingding_reg" class="dingding-style"></div>',
					id: 'dingding-title'
				});
				element.tabChange('login-handle', 'dingding-title');
				
				window.DTFrameLogin(
					{
						id: 'dingding_reg',
						width: 300,
						height: 300
					},
					{
						redirect_uri: encodeURIComponent(window.location.protocol + '//'+ window.location.host +'/dingding'),
						client_id: res.data.appKey,
						scope: 'openid',
						response_type: 'code',
						state: 'success',
						prompt: 'consent'
					},
					(loginResult) => {
						const {redirectUrl, authCode, state} = loginResult;
						// 这里可以直接进行重定向
						window.location.href = redirectUrl;
					},
					(errorMsg) => {
						// 这里一般需要展示登录失败的具体原因
						layer.msg(`Login Error: ${errorMsg}`);
					},
				);
			},
			error: function(res){
				return false;
			}
		});
		
		//初始化飞书登录
		$.ajax({
			url: '/globalConfig/query/feishu',
			type: 'POST',
			dataType: "json",
			contentType: 'application/json;charset=UTF-8',
			data: JSON.stringify({"itemType": 12}),
			success: function(res){
				//如果没有开启
				if(res.code != "000000" || !res.data || res.data.enable != 1){
					return false;
				}
				
				element.tabAdd('login-handle', {
					title: '飞书',
					content: '<div id="feishu_reg" style="text-align:center;margin-top:-9px"></div>',
					id: 'feishu-title'
				});
				element.tabChange('login-handle', 'feishu-title');
				
				var loginUrl = window.location.protocol + '//'+ window.location.host +'/feishu';
				var gotoUrl = "https://passport.feishu.cn/suite/passport/oauth/authorize?client_id="+ res.data.appID +"&redirect_uri="+loginUrl+"&response_type=code&state=success";
				var QRLoginObj = QRLogin({
					id:"feishu_reg",
					goto: gotoUrl,
					width: "100",
					height: "100",
					style: "width:251px;height:251px;border:none"
				});
				
				var handleMessage = function (event) {         
					// 使用 matchOrigin 和 matchData 方法来判断 message 和来自的页面 url 是否合法
					if(QRLoginObj.matchOrigin(event.origin) && QRLoginObj.matchData(event.data)) { 
						var loginTmpCode = event.data.tmp_code; 
						// 在授权页面地址上拼接上参数 tmp_code，并跳转
						window.location.href = `${gotoUrl}&tmp_code=${loginTmpCode}`;
					}
				};

				if (typeof window.addEventListener != 'undefined') {   
					window.addEventListener('message', handleMessage, false);} 
				else if (typeof window.attachEvent != 'undefined') { 
					window.attachEvent('onmessage', handleMessage);
				}
			},
			error: function(res){
				return false;
			}
		});
		
		//初始化CAS登录
		$.ajax({
			url: '/globalConfig/query/cas',
			type: 'POST',
			dataType: "json",
			contentType: 'application/json;charset=UTF-8',
			data: JSON.stringify({"itemType": 11}),
			success: function(res){
				//如果没有开启
				if(res.code != "000000" || !res.data || res.data.enable != 1){
					return false;
				}
				
				element.tabAdd('login-handle', {
					title: 'CAS',
					content: '',
					id: 'cas-title'
				});
			},
			error: function(res){
				return false;
			}
		});
		
		element.on('tab(login-handle)', function(data){
			if($(this).attr('lay-id') == 'cas-title'){
				window.location.href = '/page/sys_user/cas_login.html';
				//window.location.href = '/cas';
			}
		});
		
        // 普通登录
        form.on('submit(normal-login)', function(data) {
			data.field.password = data.field.password.md5(32).toUpperCase();
			$.ajax({
				url: '/sysUser/login',
				type: 'POST',
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				data: JSON.stringify(data.field),
				success: function(res){
					if(res.code != "000000"){
						layer.msg("登录失败：" + res.message, {icon: 2, shift: 6, offset: ['9%', '41%']});
						return false;
					}
					setCookie("login_token", res.data.lastLoginToken);
					window.location = '/index.html';
				},
				error: function(res){
					layer.msg("登录失败：" + res.message, {icon: 2, shift: 6, offset: ['9%', '41%']});
				}
			});
            return false;
        });
		
		// LDAP
        form.on('submit(ldap-login)', function(data) {
			$.ajax({
				url: '/sysUser/login',
				type: 'POST',
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				data: JSON.stringify(data.field),
				success: function(res){
					if(res.code != "000000"){
						layer.msg("登录失败：" + res.message, {icon: 2, shift: 6, offset: ['9%', '41%']});
						return false;
					}
					setCookie("login_token", res.data.lastLoginToken);
					window.location = '/index.html';
				},
				error: function(res){
					layer.msg("登录失败：" + res.message, {icon: 2, shift: 6, offset: ['9%', '41%']});
				}
			});
            return false;
        });
    });
</script>
</body>
</html>